import { Component, OnInit } from '@angular/core';
import {User} from './user';
import {UserService} from './user.service';
import { FormsModule,NgForm } from '@angular/forms';

@Component({
  selector: 'app-sign',
  templateUrl: './sign.component.html',
  styleUrls: ['./sign.component.css']
})

export class SignComponent implements OnInit{

  loading: boolean;
  user : any;
  nameErrorMsg:any;
  active:boolean;
  submitted = false;

  constructor(private userService: UserService) {

      this.user =  {
            name: '',
            tel: '',
            city: ''
        };
  }

  ngOnInit() {
    this.loading = true;
    this.userService.getUserService().subscribe(user => {
        //console.log(user);
        this.user = <User[]>user;
        this.loading = false;
    });
  }

  //服务端验证  异步验证

  //获取表单控件数据

  logForm(theForm: NgForm) {
     if (theForm.invalid) {
         if (theForm.controls['name'].errors) {
           //stringify 字符串转换
             this.nameErrorMsg = 'name error:' + JSON.stringify(theForm.controls['name'].errors);
         } else {
             this.nameErrorMsg = null;
         }
     }
     console.log(theForm.value);
 }

  /*
  post (userForm) { this.submitted = true; }
  //重置页面
    reset() {
      this.user = { // 重置用户数据
          city: {}
      };
      this.active = true;
      setTimeout(() => this.active = true, 0);
      return true;
  }
 */
}
